package com.huanghuai.retail.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.huanghuai.retail.common.R;
import com.huanghuai.retail.dto.BrandDto;
import com.huanghuai.retail.entity.PBrand;
import com.huanghuai.retail.service.BrandService;
import com.huanghuai.retail.service.BrandTypeService;
import com.huanghuai.retail.service.SuppliersService;
import com.huanghuai.retail.service.TypeService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/brand")
@Slf4j
public class BrandController {

    @Autowired
    private BrandService brandService;

    @Autowired
    private SuppliersService suppliersService;

    @Autowired
    private TypeService typeService;

    @Autowired
    private BrandTypeService brandTypeService;

    /**
     * 查询所有，用于新增商品时下拉框
     * @return
     */
    @GetMapping("/findAll")
    public R<List<PBrand>> findAll(){
        List<PBrand> brands = brandService.list();
        if (brands == null || brands.size() == 0)
            return R.error("查询失败");
        return R.success(brands);
    }


    /**
     * 新增品牌
     * @param pBrand
     * @return
     */
    @PostMapping("/addBrand")
    public R<String> addBrand(@RequestBody BrandDto pBrand){

        String pBrandName = pBrand.getName();
        Long factoryId = pBrand.getFactoryId();
        String linkman = pBrand.getLinkman();
        String linkmanPhone = pBrand.getLinkmanPhone();
        if (StringUtils.isNotBlank(linkman)
                &&StringUtils.isNotBlank(linkmanPhone)
                &&StringUtils.isNotBlank(pBrandName)
                &&StringUtils.isNotBlank(String.valueOf(factoryId))) {
            LambdaQueryWrapper<PBrand> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(PBrand::getName,pBrand.getName());
            PBrand one = brandService.getOne(queryWrapper);
            if (one == null){

                brandService.save(pBrand);
                return R.success("新增品牌成功");
            }
            return R.error("品牌信息已存在");
        }
        return R.error("新增失败,所有都数据都不能有空");

    }


    /**
     * 修改品牌信息
     * @param pBrand
     * @return
     */
    @PutMapping()
    public R<String> updateBrand(@RequestBody PBrand pBrand){

        Long brandId = pBrand.getId();
        if(brandId==null ||brandService.getById(pBrand.getId())==null){
            return R.error("品牌不存在");
        }
        String pBrandName = pBrand.getName();
        Long factoryId = pBrand.getFactoryId();
        String linkman = pBrand.getLinkman();
        String linkmanPhone = pBrand.getLinkmanPhone();
        if (StringUtils.isNotBlank(String.valueOf(brandId))
                &&StringUtils.isNotBlank(linkman)
                &&StringUtils.isNotBlank(linkmanPhone)
                &&StringUtils.isNotBlank(pBrandName)
                &&StringUtils.isNotBlank(String.valueOf(factoryId))){
            brandService.updateById(pBrand);
            return R.success("修改成功  ");
        }
        return R.error("修改失败,所有都数据都不能有空");

    }


    /**
     * 根据id删除品牌
     * @param id
     * @return
     */
    @DeleteMapping()
    public R<String> delete(Long id){
        if(StringUtils.isNotBlank(String.valueOf(id))&&brandService.getById(id)==null){
            return R.error("品牌不存在,删除失败");
        }
        boolean isTrue = brandService.removeById(id);
        if(isTrue) {
            return R.success("品牌删除成功");
        }
        else return R.error("删除失败");
    }


    /**
     * 根据供应商名称、品牌名称、品牌类型名称两个(品牌类型呈包含关系，如饮料，乳制品)、查询品牌
     * @param page
     * @param pageSize
     * @param suppliersName
     * @param name
//     * @param brandTypeNameOne
//     * @param brandTypeNameTwo
     * @return
     */


    @GetMapping("/page")
    public R<Page> page(@RequestParam(defaultValue= "1") int page,
                        @RequestParam(defaultValue = "10") int pageSize,
                        String suppliersName,String name,String typeFirstName,String typeSecondName){

            Page<PBrand> pageInfo  = new Page(page,pageSize);
            LambdaQueryWrapper<PBrand> queryWrapper = new LambdaQueryWrapper<>();


            //处理供应商字段
            List<Long> brandIdList = null;
            if(suppliersName!=null){
                brandIdList = suppliersService.getIdsByName(suppliersName);
            }
            if(brandIdList!=null&&brandIdList.size()==0){
                brandIdList.add(-1L);
            }
            queryWrapper.in(brandIdList!=null,PBrand::getId,brandIdList);

            //处理name字段
            queryWrapper.like(StringUtils.isNotEmpty(name),PBrand::getName,name);

            //处理type的两个字段
            List<Long> typeIds = typeService.findIdsByName(typeFirstName,typeSecondName);
            List<Long> brandIds = null;
            if(typeIds!=null){
                brandIds = brandTypeService.findIds(typeIds);
            }
            if(brandIds!=null&&brandIds.size()==0){
                brandIds.add(-1L);
            }
            queryWrapper.in(brandIds!=null,PBrand::getId,brandIds);

            queryWrapper.orderByDesc(PBrand::getId);
            brandService.page(pageInfo,queryWrapper);

            List<PBrand> brandRecords = pageInfo.getRecords();

            if (brandRecords.size()==0){
                return R.error("未查询到信息,品牌不存在!");
            }

            List<BrandDto> brandDtoList = new ArrayList<>();
            Page<BrandDto> brandDtoPage = new Page<>();
            for (int i = 0; i < brandRecords.size(); i++) {
                brandDtoList.add(brandService.solveDto(brandRecords.get(i)));
            }
            BeanUtils.copyProperties(pageInfo,brandDtoPage);
            brandDtoPage.setRecords(brandDtoList);
            return R.success(brandDtoPage);
    }


    /**
     * 查询所有的品牌信息
     * @param page
     * @param pageSize
     * @return
     */
    @GetMapping("/selectAllBrands")
    public R<Page> selectAllBrands(@RequestParam(defaultValue= "1") int page,
                                   @RequestParam(defaultValue = "10") int pageSize){

        Page<PBrand> brandPage = new Page<>(page,pageSize);
        brandService.page(brandPage);
        List<PBrand> records = brandPage.getRecords();
        List<BrandDto> brandDtoList = new ArrayList<>();
        Page<BrandDto> brandDtoPage = new Page<>();
        for (int i = 0; i <records.size(); i++) {
            brandDtoList.add(brandService.solveDto(records.get(i)));
        }

        BeanUtils.copyProperties(brandPage,brandDtoPage);
        brandDtoPage.setRecords(brandDtoList);

        return R.success(brandDtoPage);
    }

    /**
     * 根据id查询品牌信息
     * @param id
     * @return
     */
    @GetMapping("/findBrandById")
    public R<BrandDto> findBrandById(Long id){
        log.info("根据id查询品牌信息");
        PBrand pBrand = brandService.getById(id);
        if(pBrand !=null){
            BrandDto brandDto = brandService.solveDto(pBrand);
            return R.success(brandDto);
        }
        return R.error("没有查询到对应的品牌信息");
    }

    /**
     * 启用，禁用品牌
     * @param pBrand
     * @return
     */
    @PutMapping("/update")
    public R<String> update(@RequestBody PBrand pBrand){
        PBrand brand = brandService.getById(pBrand.getId());
        if (brand==null){
            return R.error("状态修改失败,供应商信息不存在");
        }
        brandService.updateById(pBrand);
        return R.success("品牌状态修改成功");
    }

}
